package org.study.itcast.java7;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author: jzhou
 * @date: 2024/4/20-23:30
 * @version: 1.0
 * @description: spark3.3-study
 */
public class PathsAndFiles {
    public static void main(String[] args) throws IOException {
        // 打印程序使用默认路径
        System.out.println(System.getProperty("user.dir"));
//        walkFileTree(); 遍历目录文件
        // 统计 jar 的数目
//        countJar();
        // 删除多级目录
//        deleteFile();

//        拷贝多级目录
        long start = System.currentTimeMillis();
        String source = "E:\\software\\gradle-4.8";
        String target = "E:\\software\\gradle-4.8aaaa";

//        copy(source, target);

        // walk 方法也遍历路径下所有文件和文件夹 返回的是一个 Stream 流
        Files.walk(Paths.get(source)).forEach(path -> {
            try {
                String targetName = path.toString().replace(source, target);
                System.out.println(targetName);
                // 是目录
                if (Files.isDirectory(path)) {
                    Files.createDirectory(Paths.get(targetName));
                }
                // 是普通文件
                else if (Files.isRegularFile(path)) {
                    Files.copy(path, Paths.get(targetName));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }

    private static void copy(String source, String target) throws IOException {
        Files.walkFileTree(Paths.get(source), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                Files.createDirectory(Paths.get(dir.toString().replace(source, target)));
                return super.preVisitDirectory(dir, attrs);
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.copy(file, Paths.get(file.toString().replace(source, target)));
                return super.visitFile(file, attrs);
            }
        });
    }


    /**
     * 删除多级目录
     * @throws IOException
     */
    private static void deleteFile() throws IOException {

        Path path = Paths.get("E:\\software\\gradle-4.8 - 副本");
        Files.walkFileTree(path, new SimpleFileVisitor<Path>(){

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println("====> 进入" + dir);
                return super.preVisitDirectory(dir, attrs);
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                    throws IOException {
                System.out.println(file);
                Files.delete(file);
                return super.visitFile(file, attrs);
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc)
                    throws IOException {
                System.out.println("<==== 退出 " + dir);
                Files.delete(dir);
                return super.postVisitDirectory(dir, exc);
            }
        });
    }

    /**
     * 统计 jar 的数目
     * @throws IOException
     */
    private static void countJar() throws IOException {
        Path path = Paths.get("E:\\software\\jdk1.8.0_261");
        AtomicInteger fileCount = new AtomicInteger();
        Files.walkFileTree(path, new SimpleFileVisitor<Path>(){
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                    throws IOException {
                if (file.toFile().getName().endsWith(".jar")) {
                    System.out.println(file);
                    fileCount.incrementAndGet();
                }
                return super.visitFile(file, attrs);
            }
        });
        System.out.println(fileCount); // 337
    }


    /**
     * 遍历目录
     * @throws IOException
     */
    private static void walkFileTree() throws IOException {
        Path path = Paths.get("E:\\software\\jdk-17.0.10");
        AtomicInteger dirCount = new AtomicInteger();
        AtomicInteger fileCount = new AtomicInteger();
//        int count = 0;
        Files.walkFileTree(path, new SimpleFileVisitor<Path>(){
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                    throws IOException {
                System.out.println(dir);
                dirCount.incrementAndGet();
//                count++;  匿名类要引用外部的局部遍历，那这个变量相当于 final 修饰的变量 ，所以不能再修改 要用局部变量可以用累加器
                return super.preVisitDirectory(dir, attrs); // 注意：return 的结果不要改 否则有肯定破坏源有逻辑
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                    throws IOException {
                System.out.println(file);
                fileCount.incrementAndGet();
                return super.visitFile(file, attrs);
            }
        });
        System.out.println(dirCount); // 133
        System.out.println(fileCount); // 1479
    }

}
